<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2019/1/30
 * Time: 5:45 PM
 */
namespace App\Controllers\Api;
use App\Exception\ParamException;
use App\Models\Entity\User;
use \Firebase\JWT\JWT;
use Swoft\Db\Query;
use Swoft\Http\Message\Server\Request;
use Swoft\Http\Server\Bean\Annotation\Controller;
use Swoft\Http\Server\Bean\Annotation\RequestMapping;
use Swoft\Http\Server\Bean\Annotation\RequestMethod;

use Swoft\Http\Message\Bean\Annotation\Middlewares;
use Swoft\Http\Message\Bean\Annotation\Middleware;
use App\Test\AccessMiddWare;
/**
 *
 * @Controller(prefix="/token")
 * @Middlewares({
    @Middleware(\AccessMiddWare::class)
 *     })
 */
class TokenController
{
    /**
     * @RequestMapping()
     */
    function access_token(Request $request){
        $uname=$request->query('uname',false);
        $usec=$request->query('usec',false);
//        获取私秘钥解密
        $prikey=cache()->get("prikey");
        openssl_private_decrypt(base64_decode($uname),$uname,$prikey);
        openssl_private_decrypt(base64_decode($usec),$usec,$prikey);

//        定义抛出异常
        if (!$uname || !$usec) throw new ParamException("错误参数");
        $getUser= Query::table(User::class)
           ->where('user_pwd',$usec)
            ->where('user_name',$uname)
            ->one()
            ->getResult();
        $key="hboa";
        if ($getUser){
//            定义token
            $token=[
                'iss'=>'www.baidu.com',
                'uname'=>$getUser,
//                过期时间
                'exp'=>time()+10000
            ];
//            对称加密
            $token=JWT::encode($token,$key);
            return ['token'=>$token];
        }else{
            return ['token'=>''];
        }
    }


    /**
     * @RequestMapping(route="verify",method={RequestMethod::GET})
     * 测试解密
     */
    function verify(Request $request){
       $getToken=$request->query("token",false);
        $key="hboa";
        $userInfo=JWT::decode($getToken,$key,["HS256"]);
       return array($userInfo);
    }

    /**
     * 模拟一个场景生成私钥token
     * @RequestMapping(route="asytoken",method={RequestMethod::GET})
     */
    function asytoken(){
//        获取私钥
        $prikey=cache()->get("prikey");
//        token加密
        $token=[
            'iss'=>'cc',
            'exp'=>time()+30,
        ];
        $jwt=JWT::encode($token,$prikey,"RS256");
        return ["token",$jwt];
    }
    /**
     * 模拟上面场景进行解密
     * @RequestMapping(route="jietoken",method={RequestMethod::GET})
     */
    function jietoken(Request $request){
        $simi=$request->query("token","");
        if ($simi){
//            获取公钥
            $pubkey=cache()->get("pubkey");
            $jwt=JWT::decode($simi,$pubkey,["RS256"]);
            return (array)$jwt;
        }
        return "no token";
    }

    /**
     * 可以通过私钥获取公钥
     * @RequestMapping(route="pubkey",method={RequestMethod::POST})
     */
    function pubkey(){
        $prikey=cache()->get("prikey");
//        鉴权
        $pubkeyObject=openssl_pkey_get_private($prikey);
        $pubkey=openssl_pkey_get_details($pubkeyObject);
        $pubkey=$pubkey['key'];
        return ['pubkey'=>$pubkey];
    }

}
